﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;

namespace SharpSoft.WPF.Converters
{
    public class BooleanLogicConverter : IMultiValueConverter
    {
        /// <summary>
        /// 逻辑运算符
        /// </summary>
        public LogicOperator Operator { get; set; } = LogicOperator.AND;
        /// <summary>
        /// 是否对运算结果取反
        /// </summary>
        public bool Negation { get; set; } = false;
        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            var result = true;
            if (values == null || values.Length == 0)
            {
                result = false;
            }
            else
            {
                bool[] bos = new bool[values.Length];
                for (int i = 0; i < values.Length; i++)
                {
                    if (values[i] is bool bo)
                    {
                        bos[i] = (bool)bo;
                    }
                    else
                    {
                        return false;
                    }

                }
                switch (Operator)
                {
                    case LogicOperator.AND:
                        foreach (var item in bos)
                        {
                            result &= item;
                        }
                        break;
                    case LogicOperator.OR:
                        foreach (var item in bos)
                        {
                            result |= item;
                        }
                        break;
                    case LogicOperator.XOR:
                        foreach (var item in bos)
                        {
                            result ^= item;
                        }
                        break;
                    default:
                        break;
                }
            }

            return Negation ? (!result) : result;
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }


        public enum LogicOperator
        {
            AND,
            OR,
            XOR,

        }
    }
}
